package com.seven.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seven.domain.Response;
import com.seven.domain.Ticket;
import com.seven.domain.TicketUploadVO;
import com.seven.service.UploadService;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * @author seven
 */
@Controller
@Slf4j
@RequestMapping("/api/v1/invoice")
public class InvoiceController {

  private final UploadService uploadService;

  private String splitChar = "\\+";

  public InvoiceController(UploadService uploadService) {
    this.uploadService = uploadService;
  }

  @PostMapping(value = "/upload")
  @ResponseBody
  public Response<Void> multiUpload(TicketUploadVO uploadVO, HttpServletRequest request) {
    log.info("请求参数：{}", uploadVO);
    List<MultipartFile> multipartFiles = ((MultipartHttpServletRequest) request).getMultiFileMap()
        .get("file");
    String groupName = uploadVO.getGroupName();
    String userInfo = uploadVO.getUserInfo();
    String[] userInfoArray = userInfo.trim().split(splitChar);
    String userName = userInfoArray[0];
    String userCode = userInfoArray[1];
    if (CollectionUtils.isEmpty(multipartFiles)) {
      return Response.fail("上传的文件为空，无法执行操作");
    }
    String msg = uploadService.upload(multipartFiles, userName, userCode, groupName);
    return Response.successMsg(msg);
  }

  @DeleteMapping(value = "/{ids}")
  @ResponseBody
  public Response<Void> multiDelete(@PathVariable("ids") String ids) {
    log.info("删除发票：{}", ids);
    List<String> idList = Arrays.asList(ids.split(","));
    uploadService.delete(idList);
    return Response.success();
  }

  @GetMapping(value = "/download/pdf/{ids}")
  public void multiDownloadPdf(@PathVariable("ids") String ids, HttpServletResponse response)
      throws IOException {
    log.info("下载发票：{}", ids);
    List<String> idList = Arrays.asList(ids.split(","));
    uploadService.download(idList, response);
  }

  @GetMapping(value = "/download/excel/{ids}")
  public void multiDownloadExcel(@PathVariable("ids") String ids, HttpServletResponse response)
      throws IOException {
    log.info("下载发票：{}", ids);
    List<String> idList = Arrays.asList(ids.split(","));
    uploadService.downloadExcel(idList, response);
  }

  @GetMapping
  @ResponseBody
  public Response<Ticket> list(@RequestParam(value = "userName",required = false) String userName,
                                     @RequestParam(value = "groupName",required = false) String groupName,
                                     @RequestParam(value = "page",required = false) Integer page,
                                     @RequestParam(value = "limit",required = false) Integer limit,
                                     @RequestParam(value = "number", required = false) String number,
                                     @RequestParam(value = "orderBy", required = false) String orderBy,
                                     @RequestParam(value = "orderDir", required = false) String orderDir){
    log.info("parm :{},{},{},{},{},{},{}", userName, groupName, page, limit, number, orderBy, orderDir);
    IPage<Ticket> ticketPage = uploadService
        .list(page, limit, userName, groupName, number, orderBy, orderDir);
    return Response.success(ticketPage);
  }

}
